In [ ]:
import networkx as nx
import matplotlib.pyplot as plt
from collections import Counter
%matplotlib inline
# Load the pickled data without the new individuals added in the previous notebook.
G = nx.read_gpickle('Synthetic Social Network.pkl')
Within a social network, there will be certain individuals which perform certain important functions. For example, there may be hyper-connected individuals who are connected to many, many more people. They would be of use in the spreading of information. Alternatively, if this were a disease contact network, identifying them would be useful in stopping the spread of diseases. How would one identify these people?
In [ ]:
# Let's find out the number of neighbors that individual #7 has.
G.neighbors(7)
Can you create a ranked list of the importance of each individual, based on the number of neighbors they have?
Hint: One suggested output would be a list of tuples, where the first element in each tuple is the node ID (an integer number), and the second element is a list of its neighbors.
Hint: Python's sorted(iterable, key=lambda x:...., reverse=True)
function may be of help here.
In [ ]:
The number of other nodes that one node is connected to is a measure of its centrality. NetworkX implements a degree centrality, which is defined as the number of neighbors that a node has normalized to the number of individuals it could be connected to in the entire graph. This is accessed by using nx.degree_centrality(G)
In [ ]:
nx.degree_centrality(G)
If you inspect the dictionary closely, you will find that node 19 is the one that has the highest degree centrality, just as we had measured by counting the number of neighbors.
There are other measures of centrality, namely betweenness centrality, flow centrality and load centrality. You can take a look at their definitions on the NetworkX API docs and their cited references. You can also define your own measures if those don't fit your needs, but that is an advanced topic that won't be dealt with here.
The NetworkX API docs that document the centrality measures are here: http://networkx.github.io/documentation/networkx-1.9.1/reference/algorithms.centrality.html
n
nodes, then how many possible edges are there in total, assuming self-edges are allowed? What if self-edges are not allowed?Time: 3-6 min.
Hint: You may want to use:
plt.hist(list_of_values)
and
plt.scatter(x_values, y_values)
If you know the Matplotlib API, feel free to get fancy :).
In [ ]:
# Your answer here.
Graph traversal is akin to walking along the graph, node by node, restricted by the edges that connect the nodes. Graph traversal is particularly useful for understanding the local structure (e.g. connectivity, retrieving the exact relationships) of certain portions of the graph and for finding paths that connect two nodes in the network.
Using the synthetic social network, we will figure out how to answer the following questions:
In [ ]:
nx.draw(G, with_labels=True)
Let's say we wanted to find the shortest path between two nodes. How would we approach this? One approach is what one would call a breadth-first search (http://en.wikipedia.org/wiki/Breadth-first_search). While not necessarily the fastest, it is the easiest to conceptualize.
The approach is essentially as such:
Try implementing this algorithm in a function called path_exists(node1, node2, G)
.
The function should take in two nodes, node1
and node2
, and the graph G
that they belong to, and return a Boolean that indicates whether a path exists between those two nodes or not.
In [ ]:
def path_exists(node1, node2, G):
"""
This function checks whether a path exists between two nodes (node1, node2) in graph G.
"""
And testing the function on a few test cases:
False
)True
)
In [ ]:
path_exists(18, 5, G)
path_exists(29, 26, G)
Meanwhile... thankfully, NetworkX has a function for us to use, titled has_path
, so we don't have to always implement this on our own. :-)
In [ ]:
nx.has_path(G, 18, 5)
NetworkX also has other shortest path algorithms implemented.
We can build upon these to build our own graph query functions. Let's see if we can trace the shortest path from one node to another.
In [ ]:
nx.draw(G, with_labels=True)
nx.shortest_path(G, source, target)
gives us a list of nodes that exist within one of the shortest paths between the two nodes. (Not all paths are guaranteed to be found.)
In [ ]:
nx.shortest_path(G, 4, 14)
Incidentally, the node list is in order as well - we will travel through 19 and 17 in that order to get from 14 from 4.
Write a function that extracts the edges in the shortest path between two nodes and puts them into a new graph, and draws it to the screen. It should also return an error if there is no path between the two nodes. (~5 min)
Hint: You may want to use G.subgraph(iterable_of_nodes)
to extract just the nodes and edges of interest from the graph G
. One coding pattern to consider is this:
newG = G.subgraph(nodes_of_interest)
newG will be comprised of the nodes of interest and the edges that connect them.
In [ ]:
# Possible Answer:
def extract_path_edges(G, source, target):
"""
Fill in the code below.
"""
# Test your function with the following block of code.
newG = extract_path_edges(G, 1, 14)
nx.draw(newG, with_labels=True)
In [ ]:
# Possible Answer
def extract_neighbor_edges(G, node):
"""
Fill in code below.
"""
# Test your function with the following block of code.
fig = plt.figure(0)
newG = extract_neighbor_edges(G, 19)
nx.draw(newG, with_labels=True)
Let's try some other problems that build on the NetworkX API. (10 min.)
Refer to the following for the relevant functions:
https://networkx.github.io/documentation/latest/reference/algorithms.shortest_paths.html
Counter
object from the collections
module combinations
function from the itertools
module.all_shortest_paths(G, node1, node2)
which is part of the networkX algorithms.
In [ ]:
# Your answer to Question 1:
# All we need here is the length of the path.
def compute_transmission_time(G, source, target):
"""
Fill in code below.
"""
# Test with the following line of code.
compute_transmission_time(G, 14, 4)
In [ ]:
# Your answer to Question 2:
# We need to know the length of every single shortest path between every pair of nodes.
# If we don't put a source and target into the nx.shortest_path_length(G) function call, then
# we get a dictionary of dictionaries, where all source-->target-->lengths are shown.
In [ ]:
# Your answer to Question 3:
# You may want to use the Counter object from collections, as well as combinations from itertools.
from collections import Counter
from itertools import combinations
In [ ]:
# Your answer to Question 4:
# Hint: You may want to use bar graphs or histograms.
plt.bar(totals.keys(), totals.values())
It looks like individual 19 is an important person of some sorts - if a message has to be passed through the network in the shortest time possible, then usually it'll go through person 19. Such a person has a high betweenness centrality. This is implemented as one of NetworkX's centrality algorithms. Check out the Wikipedia page for a further description.
In [ ]:
btws = nx.betweenness_centrality(G, normalized=False)
plt.bar(btws.keys(), btws.values())
In [ ]:
Think about it...
From the scatter plot, we can see that the dots don't all fall on the same line. Degree centrality and betweenness centrality don't necessarily correlate. Can you think of a reason why?
What would be the degree centrality and betweenness centrality of the middle connecting node in the barbell graph below?
In [ ]:
nx.draw(nx.barbell_graph(5, 1))
In [ ]: